package com.example.imtoken.modules.main.wallet

import android.content.Intent
import android.view.View
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.BusUtils
import com.blankj.utilcode.util.ClickUtils
import com.blankj.utilcode.util.ColorUtils
import com.example.imtoken.R
import com.example.imtoken.core.App
import com.example.imtoken.core.OnSwitchWalletListener
import com.example.imtoken.core.base.BaseFragment
import com.example.imtoken.core.utils.BusConfig
import com.example.imtoken.databinding.WalletFragmentBinding
import com.example.imtoken.modules.help_h5.HelpH5Activity
import com.example.imtoken.modules.main.wallet.assets.add.AddAssetsActivity
import com.example.imtoken.modules.main.wallet.details.trade.code.ReceivingCodeActivity
import com.example.imtoken.modules.main.wallet.manage.ManageActivity
import com.example.imtoken.modules.main.wallet.select.SelectWalletDialogFragment
import com.example.imtoken.modules.scanning.ScanningActivity
import kotlinx.android.synthetic.main.wallet_fragment.*

/**
 * 钱包
 */
class WalletFragment :
    BaseFragment<WalletViewModel, WalletFragmentBinding>(WalletViewModel::class.java),
    OnSwitchWalletListener {

    override fun initLayoutId(): Int {
        return R.layout.wallet_fragment
    }

    override fun init() {
        BusUtils.register(this)
        initView()
        initObserve()
        initListener()
        initClick()
        viewModel.getDefAccount()
    }

    private fun initClick() {
        ClickUtils.applyGlobalDebouncing(dataBinding.llEnergy){
            val intent = Intent(activity, HelpH5Activity::class.java)
            intent.putExtra("title", getString(R.string.tronManage))
            intent.putExtra("url", viewModel.getCoinResource().value?.a_href)
            ActivityUtils.startActivity(intent)
        }
        ClickUtils.applyGlobalDebouncing(ivAdd) {
            ActivityUtils.startActivity(AddAssetsActivity::class.java)
        }
        ClickUtils.applyGlobalDebouncing(ivMore2) {
            ActivityUtils.startActivity(ManageActivity::class.java)
        }
        ClickUtils.applyGlobalDebouncing(dataBinding.linearLayout) {
            //收款二维码
            val intent = Intent(activity, ReceivingCodeActivity::class.java)
            intent.putExtra("address", App.createLiveData.value?.coin?.address)
            intent.putExtra("title", App.createLiveData.value?.coin?.coin)
            ActivityUtils.startActivity(intent)
        }
        ClickUtils.applyGlobalDebouncing(dataBinding.ivScanning) {
            //扫描
            ActivityUtils.startActivity(ScanningActivity::class.java)
        }
    }

    @BusUtils.Bus(tag = BusConfig.update_default_account_sticky_wallet_fragment, sticky = true)
    fun updateDefAccount() {
        viewModel.getDefAccount()
        BusUtils.postSticky(BusConfig.update_wallet_address_sticky_assets_fragment)
    }

    private fun initView() {
        walletViewPager2.adapter = WalletAdapter(childFragmentManager, lifecycle)
        walletViewPager2.offscreenPageLimit = 1
        walletViewPager2.currentItem = 0
        walletViewPager2.isUserInputEnabled = false
    }

    private fun initObserve() {
        viewModel.getCoinResource().observe(this) {
            dataBinding.coinResource = it
        }
        viewModel.getDefAccountData().observe(this) {
            dataBinding.entity = it

            when (it.coin) {
                "eth" -> {
                    dataBinding.llTRXView.visibility = View.GONE
//                    dataBinding.ivAdd.visibility = View.VISIBLE
                }
                "trx" -> {
                    dataBinding.llTRXView.visibility = View.VISIBLE
//                    dataBinding.ivAdd.visibility = View.GONE

                    viewModel.getCoinResource(it.coin)
                }
            }
        }
        viewModel.getBalanceData().observe(this) {
            dataBinding.balance = it
        }
    }

    private fun initListener() {
        tvAssets.setOnClickListener {
            tvAssets.setTextColor(ColorUtils.getColor(R.color.black))
            tvCollection.setTextColor(ColorUtils.getColor(R.color.light_grey))
            walletViewPager2.currentItem = 0
        }
        tvCollection.setOnClickListener {
            tvAssets.setTextColor(ColorUtils.getColor(R.color.light_grey))
            tvCollection.setTextColor(ColorUtils.getColor(R.color.black))
            walletViewPager2.currentItem = 1
        }
        walletViewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                when (position) {
                    0 -> ivAdd.visibility = View.VISIBLE
                    1 -> ivAdd.visibility = View.GONE
                }
            }
        })
        ClickUtils.applyGlobalDebouncing(ivMore) {
            SelectWalletDialogFragment(this).show(childFragmentManager, "dialog")
        }
    }

    override fun switchWallet() {
        viewModel.getDefAccount()
        BusUtils.postSticky(BusConfig.update_wallet_address_sticky_assets_fragment)
    }

    override fun onDestroy() {
        super.onDestroy()
        BusUtils.removeSticky(BusConfig.update_default_account_sticky_wallet_fragment)
        BusUtils.unregister(this)
    }

}